<?php
require_once("login/class.session.php");
require_once("functions.php");
if (!isset($session->logged_in) || !$session->logged_in) {
  header("Location: ../createaccount.php");
}

$userinfo = $session->userinfo;

if(!($userinfo['isadmin'])) {
  header("Location: ../createaccount.php");
}


$admins = query_select("
  SELECT *
  FROM user
  WHERE isadmin = 1
");

$users = query_select("
  SELECT *
  FROM user
  WHERE isadmin = 0
");

?>

<?php require_once("header.php"); ?>

<div class = "box">

<h3>Admins</h3>
<table>
<tr class="title">
<td class="padding">Username</td>
<td class="padding">Email</td>
<td class="padding">Display Name</td>
<td class="padding">Demote</td>
</tr>
<?php
  foreach($admins as $i){
    echo '<tr>';
    echo '<td class="padding">';
    echo $i['username'];
    echo '</td>';
    echo '<td class="padding">';
    echo $i['email'];
    echo '</td>';
    echo '<td class="padding">';
    echo $i['displayname'];
    echo '</td>';
    if($i['id'] != $userinfo['id']){
      echo '<td><a href="admin/dochangeusertype.php?id=' . $i['id'] . '&new=user"><img src = "layout/demote.png"/></a></td>';
    }
    else {
      echo '<td class="padding">';
      echo ' (This is you!)';
      echo '</td>';
    }
    echo '</tr>';
  }
?>
</table>
</div>

<div class = "box margin">
<h3>Users:</h3>
<table>
<tr class="title">
<td class="padding">Username</td>
<td class="padding">Email</td>
<td class="padding">Display Name</td>
<td class="padding">Promote</td>
<td class="padding">Delete</td>
</tr>
<?php
  foreach($users as $i){
    echo '<tr>';
    echo '<td class="padding">';
    echo $i['username'];
    echo '</td>';
    echo '<td class="padding">';
    echo $i['email'];
    echo '</td>';
    echo '<td class="padding">';
    echo $i['displayname'];
    echo '</td>';
    echo '<td class="padding">';
    echo ' <a href="admin/dochangeusertype.php?id=' . $i['id'] . '&new=admin"><img src = "layout/promote.png"/></a>';
    echo '</td>';
    echo '<td class="padding">';
    echo ' <a href="admin/dodeleteuser.php?id=' . $i['id'] . '"><img src = "layout/delete_icon.gif"/></a>';
    echo '</td>';
    echo '</tr>';
  }
?>
</table>
</div>

<?php require_once("./footer.php"); ?>
